package edu.yourl.web;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import edu.yourl.application.YourlSession;
import edu.yourl.domain.User;
import edu.yourl.presenter.IBasePagePresenter;
import edu.yourl.web.components.SearchBar;

public class BasePage extends WebPage  implements IBasePage {

	private FeedbackPanel feedbackPanel;
	
	@SpringBean(name="basePagePresenter")
	private IBasePagePresenter presenter;
	
	
	public BasePage() {
		this(null);
	}

	@SuppressWarnings("serial")
	public BasePage(PageParameters parameters) {
		
		super(parameters);
		
		if (!this.getYourlSession().isSignedIn()) {
			setResponsePage(SignInPage.class);
		}
		
		add(new BookmarkablePageLink<String>("homeLink", HomePage.class));
		add(new SearchBar("searchBar"));
		add(new ButtonBar("buttonBar") {
			@Override
			protected String getCurrentUser() {
				User currentUser = presenter.getCurrentUser(BasePage.this);
				return currentUser != null ? currentUser.getFavoritableName() : null;
			}

			@Override
			protected void removeUserInfo() {
				getYourlSession().removeUserInfo();
				setResponsePage(SignInPage.class);
			}
		});
		
		this.feedbackPanel = new FeedbackPanel("feedbackPanel");
		this.feedbackPanel.setOutputMarkupPlaceholderTag(true);
		add(feedbackPanel);
		
	}
	
	
	public YourlSession getYourlSession() {
		return (YourlSession) super.getSession(); 
	}
	
	public String getCurrentUserNick() {
		return getYourlSession().getUserNick();
	}
	
	public FeedbackPanel getFeedbackPanel() {
		return this.feedbackPanel;
	}
	
}
